// 新增一个品种
function addVariety(){
	var num_of_variety = $('input[name="variety_name[]"]').length + 1; // 即将添加的品种编号

	// 添加品种行的HTML
	var trVariety = '<tr id="variety-'+num_of_variety+'">'+
		'<td>'+num_of_variety+'</td>'+
		'<td><input type="text" name="variety_name[]" class="input-small validate[required]" id="variety-name-'+num_of_variety+'" autocomplete="off"></td>'+
		'<td><div class="input-append"><input type="text" name="variety_area[]" class="input-mini validate[required,custom[number],min[0]]" id="variety-area-'+num_of_variety+'" autocomplete="off"><span class="add-on">亩</span></div></td>'+
		'<td><select name="variety_usage_select[]" class="validate[required] variety-usage input-small" id="variety-usage-select-'+num_of_variety+'">'+
				'<option value="">请选择</option>'+
				'<option value="鲜食">鲜食</option>'+
				'<option value="酿酒">酿酒</option>'+
				'<option value="制干">制干</option>'+
				'<option value="制汁">制汁</option>'+
				'<option value="other">其它用途</option>'+
			'</select><input type="text" name="variety_usage[]" class="input-small validate[required, custom[chinese]" id="variety-usage-'+num_of_variety+'" style="display:none;" autocomplete="off" placeholder="请填写用途">'+
		'</td>'+
	'</tr>';

	// 添加收益行的HTML
	var trOutput = '<tr id="output-'+num_of_variety+'">'+
		'<td>'+num_of_variety+'</td>'+
		'<td>'+
			'<input type="text" class="input-small uneditable-input" name="output_variety[]" id="output-variety-'+num_of_variety+'" disabled>'+
		'</td>'+
		'<td>'+
			'<div class="input-append">'+
				'<input type="text" name="output_production[]" id="output-production-'+num_of_variety+'" class="input-mini validate[required,custom[integer],min[0]" autocomplete="off"><span class="add-on">斤</span>'+
			'</div>'+
		'</td>'+
		'<td>'+
			'<div class="input-append input-prepend">'+
				'<span class="add-on">每斤</span><input type="text" name="output_unit_price[]" id="output-unit-price-'+num_of_variety+'" class="input-xmini validate[required,custom[number],min[0]" autocomplete="off"><span class="add-on">元</span>'+
			'</div>'+
		'</td>'+
	'</tr>';

	$("#varieties").append(trVariety);	// 品种表格增加一行
	$("#output").append(trOutput);	// 收益表格增加一行
	$('[id="variety-usage-select-'+num_of_variety+'"]').change(otherInput);	// 绑定品种用途select和其它text
	$('#variety-name-'+num_of_variety+'').keyup(function(e){	// 绑定两个品种名text
				$('#output-variety-'+num_of_variety+'').val($(this).val());
			});
}

// 减少最后一个品种
function removeVariety(){
	var current_num_of_variety = $('input[name="variety_name[]"]').length; // 即将添加的品种编号
	$("#variety-"+current_num_of_variety).remove();
	$("#output-"+current_num_of_variety).remove();
}

// 验证品种表格
function validateVariety(){
	return _validateVarietyDup();
}

// 验证重复[品种名, 用途]
function _validateVarietyDup(){
	var name_inputs = $('input[name="variety_name[]"]');
	var usage_inputs = $('input[name="variety_usage[]"]');

	var total_variety_num = name_inputs.length;

	var strAry = [];
	var dup_index = 0;

	for (var i = 0; i < total_variety_num; i++) {
		strAry[i] = $.trim(name_inputs[i].value) + usage_inputs[i].value;
	};

	for (var i = 0; i < strAry.length; i++) {
		for (var j = i + 1; j < strAry.length; j++)
			if(strAry[i] == strAry[j]) {
				dup_index = j; break;
			}
	};

	if(dup_index != 0) {
		// alert("first duplicate index: "+dup_index+", total_variety_num "+total_variety_num);
		$(name_inputs[dup_index]).validationEngine('showPrompt', "* 请不要重复添加相同用途的同一品种", "error", true);
		return false;
	}
	return true;
}

// 验证当前品种信息是否填写完全
function _validateVarietyGroupEmpty(){
	// 先隐藏其它提示
	// $('input[name="variety_name[]"]').validationEngine('hide');

	var name_inputs = $('input[name="variety_name[]"]');

	var total_variety_num = name_inputs.length;

	for(var i = 1; i <= total_variety_num; i++) {
		name_is_valid = !($('[id="variety-name-' + i + '"]').validationEngine('validate')); // 验证单项失败返回true
		area_is_valid = !($('[id="variety-area-' + i + '"]').validationEngine('validate'));
		usage_select_is_valid = !($('[id="variety-usage-select-' + i + '"]').validationEngine('validate'));
		usage_is_valid = !($('[id="variety-usage-' + i + '"]').validationEngine('validate')); 
		if ((name_is_valid && area_is_valid && usage_select_is_valid && usage_is_valid) == false) {
			// 有未填项
			return false; // 有空缺项返回false
		};
	}
	return true;	// 填写完整返回true
}

